<template>
    <el-form ref="ruleFormRef" style="width: 100%" :model="ruleForm" :rules="rules" label-width="auto" class="demo-ruleForm" :size="formSize" status-icon >
        <el-form-item label="资料名称" prop="title">
            <el-input v-model="ruleForm.title" />
        </el-form-item>
        <el-form-item label="说明" prop="content">
            <el-input v-model="ruleForm.content" :rows="4" type="textarea" :autosize="{ minRows: 4 }" />
        </el-form-item>
        <el-form-item label="交付方" prop="recipient">
            <el-input v-model="ruleForm.recipient" />
        </el-form-item>
        <el-form-item label="文件" prop="files">
            <el-button type="primary" @click="onUpload" >上传</el-button>
            <el-button type="primary" @click="onShowFile(ruleForm.files)" >查看</el-button>
        </el-form-item>
        <el-form-item label="交付日期" prop="delivery_date">
            <el-date-picker v-model="ruleForm.delivery_date" type="date" value-format="YYYY-MM-DD" placeholder="请选择日期" style="width: 100%;" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
            <el-input v-model="ruleForm.remark" />
        </el-form-item>
    </el-form>
    <el-divider />
    <div class="text-center">
        <el-button type="primary" @click="submitForm(ruleFormRef)" :loading="loading"> 保存 </el-button>
    </div>

    <el-dialog v-model="showUpload" title="文件">
        <component :is="uploadFile" :data="uploadData" @saveUpload="saveUpload" />
    </el-dialog>

    <el-dialog v-model="isShowFile" title="文件" width="80%" @close="onCloseFile">
        <component :is="showFile" :data="files"/>
    </el-dialog>
</template>
  
<script lang="ts" setup>
import { reactive, ref,watch } from 'vue'
import { ElMessage, type ComponentSize, type FormInstance, type FormRules } from 'element-plus'
import uploadFile from '@/components/uploadFile.vue'
import showFile from '@/components/showFile.vue'
import http from '../../../../util/request';
const loading = ref<boolean>(false)
const showUpload = ref<boolean>(false)
let uploadData:any = ref({prefix:'project/'})
const files = ref('')
const isShowFile = ref<boolean>(false)
interface Types {
    id: number
    title: string
}

interface RuleForm {
    id: number
    project_id:number
    type_id:number|string
    title: string
    content: string
    files: string
    recipient: string
    delivery_date: string
    remark: string
}
const typeData = ref<Types[]>([])
const formSize = ref<ComponentSize>('large')
const ruleFormRef = ref<FormInstance>()
const ruleForm = ref<RuleForm>({
    id: 0,
    project_id:0,
    type_id:0,
    title: '/',
    content:'/',
    files: '',
    recipient: '',
    delivery_date: '',
    remark:'/'
})

const onGetData = () => {
    if(!ruleForm.value.id){
        return;
    }
    http.get('/completion/'+ruleForm.value.id).then((response:any)=>{
        if(response.data.data){
            ruleForm.value = response.data.data
        }
    })
}

const onGetType = () => {
    http.post('/completion/type',{}).then((response:any)=>{
        typeData.value = response.data.data
    })
}

const props = defineProps<{
    data:any
}>()

watch(props,()=>{
    ruleForm.value.id = props.data
    ruleForm.value.type_id = ''
    ruleForm.value.title = '/',
    ruleForm.value.content = '/',
    ruleForm.value.remark = '/',
    ruleForm.value.recipient = '',
    ruleForm.value.files = '',
    ruleForm.value.delivery_date =  '',
    onGetData()
    onGetType()
    
},{ immediate: true })

const rules = reactive<FormRules<RuleForm>>({
    title: [
        { required: true, message: '请输入资料名称', trigger: 'blur' },
    ],
    content: [
        { required: true, message: '请输入说明', trigger: 'blur' },
    ],
    recipient: [
        { required: true, message: '请输入交付方', trigger: 'blur' },
    ],
    files: [
        { required: true, message: '请选择文件', trigger: 'blur' },
    ],
    delivery_date: [
        { required: true, message: '请选择交付日期', trigger: 'blur' },
    ],
})

const submitForm = async (formEl: FormInstance | undefined) => {
    console.log(ruleForm.value)
    if (!formEl) return
    await formEl.validate((valid, fields) => {
        if (valid) {
            loading.value = true
            let request:any = null
            if(ruleForm.value.id){
                request = http.patch('/completion',ruleForm.value)
            }else{
                request = http.put('/completion',ruleForm.value)
            }
            request.then((response:any)=>{
                loading.value = false
                if(response.data.code != 200){
                    ElMessage.error(response.data.msg);
                    return
                }
                onSave()
            })      
        } else {
            console.log('error submit!', fields)
        }
    })
}

const onCloseFile = () => {
    files.value = ''
}
const onShowFile = (file:string) => {
    files.value = file
    isShowFile.value = true
}

const onSave = () => {
    emit('onSave')
}

const emit = defineEmits(['onSave'])

const onUpload = () => {
    uploadData.value.file = ruleForm.value.files
    showUpload.value = true
}

const saveUpload = (data:string) => {
    ruleForm.value.files = data
    showUpload.value = false
}
</script>
<style scoped>
.el-tab-pane{height: calc(100vh - 25vh);overflow-y: scroll;}
::v-deep .el-form-item .el-form-item__label {
  font-size: 16px; /* 或者你想要的任何大小 */
}
::v-deep .el-tabs__item{font-size: 18px !important;}
</style>
